package com.janwen.spring.trx.declarative;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;


@Service("serviveA")
public class ServiceAImpl implements ServiceA {

	
	@Autowired
	private DataSource dataSource;
	@Autowired
	private FooService fooService;
	@Override
	public Foo getFoo(String fooName) {
		String sql = "SELECT * FROM stuff WHERE name=?";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		List<Map<String,Object>> fooList = jdbcTemplate.queryForList(sql,fooName);
		fooService.getFoo(fooName);
		if(fooList != null && fooList.size() > 0){
			Map<String,Object> fooMap = fooList.get(0);
			Foo foo = new Foo();
			foo.setAge((Integer)fooMap.get("age"));
			foo.setName(fooMap.get("name").toString());
			return foo;
		}
		return null;
	}

	@Override
	public void updateFoo(Foo foo) {
		try {
			JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
			String sql  = "update stuff SET name=?,age=?";
			jdbcTemplate.update(sql,foo.getName(),foo.getAge()+2);
			Integer.parseInt("2a");
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
